package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.OrdersDao;
import modle.Country;
import modle.Orders;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Toolkit;

public class Search_Orders extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField Status;
	private JTextField Customer;
	private JLabel sobi_Label;
	private JLabel soto_Label;
	private JLabel foto_Label;
	private JLabel cioto_Label;
	private JButton btnSearch;
	private JButton btnBack;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Search_Orders frame = new Search_Orders();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Search_Orders() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Search_Orders.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 577, 630);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(Search_Orders.class.getResource("/img/orders2.jpg")));
		lblNewLabel_1.setBounds(210, 13, 154, 136);
		contentPane.add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setBounds(260, 162, 154, 35);
		contentPane.add(textField);
		textField.setColumns(10);
		
		 sobi_Label = new JLabel("Search Orders by Id:");
		sobi_Label.setForeground(new Color(70, 130, 180));
		sobi_Label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		sobi_Label.setBounds(33, 162, 225, 33);
		contentPane.add(sobi_Label);
		
		 soto_Label = new JLabel("Status of the order:");
		soto_Label.setForeground(new Color(70, 130, 180));
		soto_Label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		soto_Label.setBounds(33, 262, 279, 33);
		contentPane.add(soto_Label);
		
		 foto_Label = new JLabel("Feedback of the order:");
		foto_Label.setForeground(new Color(70, 130, 180));
		foto_Label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		foto_Label.setBounds(33, 352, 279, 33);
		contentPane.add(foto_Label);
		
		 cioto_Label = new JLabel("Customer's id of the order:");
		cioto_Label.setForeground(new Color(70, 130, 180));
		cioto_Label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		cioto_Label.setBounds(33, 308, 279, 33);
		contentPane.add(cioto_Label);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 400, 531, 128);
//		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//		scrollPane.setHorizontalScrollBarPolicy(DISPOSE_ON_CLOSE);
		contentPane.add(scrollPane);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
//		textArea.set
		scrollPane.setViewportView(textArea);
		
		btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnSearch.setBounds(428, 162, 106, 35);
		contentPane.add(btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SearchOrders();
			}
		});
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(14, 544, 83, 27);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new View_Orders().setVisible(true);
				dispose();
			}
		});
		
		Status = new JTextField();
		Status.setBounds(321, 265, 93, 33);
		contentPane.add(Status);
		Status.setColumns(10);
		
		Customer = new JTextField();
		Customer.setColumns(10);
		Customer.setBounds(321, 308, 93, 33);
		contentPane.add(Customer);
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(436, 13, 98, 32);
		contentPane.add(comboBox);
		
		fillCountries();
	}

	protected void SearchOrders() {
		// TODO Auto-generated method stub
		if(!StringUtil.isNum(textField.getText()))
		{
			JOptionPane.showMessageDialog(null, "请输入正确的订单号！");
			return;
		}
		
		if (StringUtil.isEmpty(textField.getText())) {
			JOptionPane.showMessageDialog(null, "ID不能为空！");
			return;
		}
		
		int id = Integer.valueOf(textField.getText());
		
		DbUtil dbUtil = new DbUtil();
		Connection con = null;
		
		try {
			con = dbUtil.getCon();
			Orders orders = OrdersDao.OrdersSearch(con, id);
			
			if(orders==null)
			{
				JOptionPane.showMessageDialog(null, "请输入正确的订单号！");
				return;
			}
			
			if(orders.getStatus()==0)   Status.setText("false");
			else   Status.setText("true");
			Customer.setText(String.valueOf(orders.getCustomer()));
			textArea.setText(orders.getFeedback());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		
		sobi_Label.setText(rd.getString("sobi_Label"));
		soto_Label.setText(rd.getString("soto_Label"));
		foto_Label.setText(rd.getString("foto_Label"));
		cioto_Label.setText(rd.getString("cioto_Label"));
		btnSearch.setText(rd.getString("btnSearch"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
